package net.moonj.UDPUI.udp;

import java.nio.charset.Charset;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.DatagramPacket;
import net.moonj.UDPUI.App;

public class UdpEchoHandler extends SimpleChannelInboundHandler<DatagramPacket> {

	@Override
	protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
		String recevied = msg.content().toString(Charset.forName("utf-8"));
		if(!recevied.startsWith("echo:")){
			App.handlePacket(msg.sender().getHostName(), msg.sender().getPort(), recevied);
			if(App.echoOpen()){
				App.instance().udpServer.sendViaChannel("echo:"+recevied, msg.sender().getHostName(), msg.sender().getPort());
			}
		}else{
			App.instance().receivedEchoCount.incrementAndGet();
			App.instance().changeCountDisplay();
		}
	}

}
